package com.lagou.edu.controller;

import com.lagou.edu.pojo.Resume;
import com.lagou.edu.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/resume")
public class ResumeController {

    @Autowired(required = false)
    private ResumeService resumeService;


    @RequestMapping("/index")
    public String index() throws Exception {
        return "login";
    }


    //用户名和密码固定为admin/admin
    @RequestMapping("/login")
    public String login(String account, String password, Map<String, Object> map, HttpServletResponse response) throws Exception {
        System.out.println(account + "=======" + password);
        if (!"admin".equalsIgnoreCase(account) || !"admin".equalsIgnoreCase(password)) {
            map.put("msg", "账号或者密码错误");
            return "login";
        }
        Cookie loginUser = new Cookie("loginUser", account);
        loginUser.setMaxAge(1800);
        response.addCookie(loginUser);
        return "redirect:list";
    }

    @RequestMapping("/get")
    public String get(Long id, Map<String, Object> map) throws Exception {
        if (id != null) {
            map.put("resume", resumeService.get(id));
        }
        return "save";
    }

    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    public String save(Resume resume) throws Exception {
        System.out.println(resume);
        if (resume != null) resumeService.save(resume);
        return "redirect:list";

    }

    @RequestMapping("/delete")
    public String delete(Long id) throws Exception {
        if (id != null) resumeService.delete(id);
        return "redirect:list";
    }

    @RequestMapping("/list")
    private String list(Map<String, Object> map) throws Exception {
        map.put("resumes", resumeService.list());
        return "list";
    }


}
